//
//  ZTDownDetailViewController.m
//  XMLYFM
//
//  Created by dllo on 15/12/28.
//  Copyright © 2015年 zzl. All rights reserved.
//

#import "ZTDownDetailViewController.h"
#import "AppTools.h"
#import "DownModel.h"
#import "TZfenLeiModel.h"
#import "TZColorScrollLabel.h"
#import "fenLeicollection.h"

#import "ZTAlbumDetailViewController.h"

#define WIDTH [[UIScreen mainScreen] bounds].size.width

#define HEIGHT [[UIScreen mainScreen] bounds].size.height

@interface ZTDownDetailViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,FenLeiTableCellDelegate>

@property (nonatomic, strong) NSMutableArray *tableViewArray;

@property (nonatomic, strong) UITableView *currentTableV;

@property (nonatomic, assign) NSInteger currentIndex;

@property (nonatomic, assign) NSInteger currentPage;

@property (nonatomic, strong) NSMutableArray *httpArray;

@property (nonatomic, assign) NSInteger currentTag;

@property (nonatomic, strong) NSMutableArray *keyArray;

@property (nonatomic, strong) NSMutableArray *imagesURLStrings;

@property (nonatomic, strong) NSMutableArray *titleArray;

@property (nonatomic, strong) UIScrollView *homeView;




//添添添 卓卓卓  的属性

// 小scrollView;
@property(nonatomic,strong)UIScrollView *smallScrollView;
@property(nonatomic,strong)NSMutableArray *allMyArray;
@property (nonatomic,strong)UICollectionView *myCollectionView;
@end

@implementation ZTDownDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self setAutomaticallyAdjustsScrollViewInsets:YES];
#pragma mark - 设置左按钮
    UIImage *leftImage = [UIImage imageNamed:@"icon_back_h.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    
    [self creatSubViews];
    [self getData];
    
    
}



-(void)creatSubViews
{
    
    //数组初始化
    self.allMyArray = [NSMutableArray array];

    
    
    //小scroll
    self.smallScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, TZWIDTH, 40)];
    self.smallScrollView.backgroundColor = [UIColor colorWithRed:0.158 green:0.923 blue:0.848 alpha:1.000];
    self.smallScrollView.showsHorizontalScrollIndicator = NO;
    self.smallScrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.smallScrollView];

    
    //创建collectionView 当容器
     UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0,0,0,0);
    flowLayout.itemSize = CGSizeMake(TZWIDTH, TZHEIGHT - 40);
    
    self.myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,40, TZWIDTH, TZHEIGHT - 40) collectionViewLayout:flowLayout];
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myCollectionView];
    self.myCollectionView.dataSource = self;
    self.myCollectionView.delegate = self;
    [self.myCollectionView registerClass:[fenLeicollection class] forCellWithReuseIdentifier:@"fenLeicollection"];
    self.myCollectionView.pagingEnabled = YES;
    
    // 设置滚动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    
}





//数据请求
-(void)getData
{
    
NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/tagsWithoutCover?categoryId=%@&contentType=album&device=iphone", self.categoryId];
//    NSLog(@"sssss%@",urlStr);
[AppTools getDataWithURL:urlStr Block:^(id result) {
    NSDictionary *Dic = result;
    
    NSMutableArray *array = Dic[@"list"];
    for (NSMutableDictionary *dic in array) {
        TZfenLeiModel *fenleiModel = [[TZfenLeiModel alloc]init];
        [fenleiModel setValuesForKeysWithDictionary:dic];
        [self.allMyArray addObject:fenleiModel];
    
    }
    
    [self addLableCount:self.allMyArray.count];
    [self.myCollectionView reloadData];
    
} ErrorBlock:^(NSError *error) {
    
    
}];



}



//添加滑动标题栏目
-(void)addLableCount:(NSInteger)count
{
   
        self.smallScrollView.contentSize = CGSizeMake(85*count, 40);
    
    for (int i = 0; i < count; i ++) {
        CGFloat lblW = 85;
        CGFloat lblH = 40;
        CGFloat lblY = 0;
        CGFloat lblX = i * lblW;
        TZColorScrollLabel *titleLabel = [[TZColorScrollLabel alloc]init];
        TZfenLeiModel *model = self.allMyArray[i];
//        titleLabel.backgroundColor = [UIColor redColor];
        titleLabel.text = model.tname;
        
        titleLabel.frame = CGRectMake(lblX, lblY, lblW, lblH);
        titleLabel.font = [UIFont systemFontOfSize:20];
        titleLabel.tag = i;
        [self.smallScrollView addSubview:titleLabel];
        titleLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tagAction:)];
        [titleLabel addGestureRecognizer:tap];

    }



}

//标题栏目label 的点击事件

-(void)tagAction:(UITapGestureRecognizer*)tap
{
    
    TZColorScrollLabel *titleLable = (TZColorScrollLabel *)tap.view;
    CGFloat offsetX = titleLable.tag * self.myCollectionView.frame.size.width;
    CGFloat offsetY = self.myCollectionView.contentOffset.y;
    CGPoint offset = CGPointMake(offsetX, offsetY);
    
    [self.myCollectionView setContentOffset:offset animated:YES];
    
    
    
}



//collectionView协议方法


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{
  return   self.allMyArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TZfenLeiModel *fenLei = self.allMyArray[indexPath.row];
    
   static NSString *cellID = @"fenLeicollection";
    
    fenLeicollection *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
      cell.categoryId = self.categoryId;
    cell.fenleiDeleget = self;
    cell.Title = fenLei.tname;
    
    return cell;
    
}



-(void)JumpMusic:(NSString *)albumId Title:(NSString *)myTitle Model:(ZTFavorModel *)model
{
    ZTAlbumDetailViewController *ztmus = [[ZTAlbumDetailViewController alloc]init];
    ztmus.myTitle = myTitle;
    ztmus.albumId = albumId;
    ztmus.favorModel = model;
     [(ZHTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:ztmus];
    [self.navigationController pushViewController:ztmus animated:YES];




}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat value = ABS(scrollView.contentOffset.x/TZWIDTH);
    
;
    NSInteger leftIndex = (int)value;
    NSInteger rightIndex = leftIndex +1;
    CGFloat scaleRight = value - leftIndex;
    CGFloat scaleLeft = 1-scaleRight;
    TZColorScrollLabel *lableLeft = self.smallScrollView.subviews[leftIndex];
    lableLeft.scale = scaleLeft;
    
    // 考虑到最后一个板块，如果右边已经没有板块了 就不在下面赋值scale了
    if (rightIndex < self.smallScrollView.subviews.count) {
        TZColorScrollLabel *labelRight = self.smallScrollView.subviews[rightIndex];
        labelRight.scale = scaleRight;
    }
    if (value >= 1)
    {
        self.smallScrollView.contentOffset = CGPointMake((scrollView.contentOffset.x-scrollView.frame.size.width*1)/scrollView.frame.size.width*85, 0);
        
//        self.smallScrollView.contentOffset = CGPointMake(value * 85, 40);
    }
    
    
}



#pragma mark - 左按钮 : 返回
- (void)leftButtonAction:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
